# ucd-snmp common sub lib

require '../web-lib.pl';
#require '/usr/sbin/syslog.pl';

&init_config();
$|=1;

########################################################
# ReadConfFile
# read file content to array, return the array reference
########################################################
sub ReadConfFile
{
$file = &read_file_lines('/etc/snmp/snmpd.conf');
$number = 0;

for($i=0; $i<@$file; $i++)
{
    $line = $file->[$i];
    $line =~ s/\r|\n//g;
    $line =~ s/^#.*$//g;
    $line =~ s/^[ \t]+//g;
    $line =~ s/[ \t]+/ /g;

    if( $line )
    {
	local $si = $i;

	while($line =~ /\\$/)
	{
		local $nl = $file->[++$i];
		$line =~ s/\\$//;
		$nl =~ s/^\s+//;
		$line .= $nl;
	}
	$item[$number++] = $line;
    }

}

$content = \@item;
return $content;
}


########################################################
# WriteConfFile($keyElem,$entry)
# search by the keyElem to replace the line with $entry
# if $entry!=NULL, add an entry or revise an entry
# if $entry=NULL, remove an entry
# return 0 if successful,else 1
########################################################
sub WriteConfFile
{
$keyElem = $_[0];
$entry = $_[1];

#initial to failed status
$code = 1;

&lock_file($config{'config_file'});
$file = &read_file_lines($config{'config_file'});


for($i=0; $i<@$file; $i++)
{
	$file->[$i] =~ s/^#.*$//;
	$file->[$i] =~ s/^[ \t]+//;
	$file->[$i] =~ s/\r|\n//;
	$file->[$i] =~ s/[ \t]+/ /;

	if($file->[$i] =~ /\b$keyElem\b/)
	{
		$code = 0;
		if($entry)
		{
			$file->[$i] =~ s/.*/$entry/;
		}
		else
		{
		#remove an entry
			$file->[$i] =~ s/.*//;
		}
		#print $file->[$i];
		last;
	}
}

if($code && $entry)
{
	$code = 0;
	push @$file, $entry;
}


&flush_file_lines();
&unlock_file($config{'config_file'});
return $code;
}


